package com.yunji.order.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunji.base.dto.PayoutFieldDTO;
import com.yunji.common.annotation.DataScope;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.base.IModifyStatusBaseBO;
import com.yunji.order.bo.GetAuditApplyBO;
import com.yunji.order.dao.IPayoutDao;
import com.yunji.order.domain.PayoutDO;
import com.yunji.order.dto.PayoutPageDTO;
import com.yunji.order.mapper.IPayoutMapper;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/16 15:28
 * @description : TODO
 **/
@Repository
public class PayoutDaoImpl extends BaseDaoImpl<IPayoutMapper, PayoutDO> implements IPayoutDao {
    @Override
    public PayoutDO getByCode(String payoutCode) {
        return lambdaQuery().eq(PayoutDO::getPayoutCode, payoutCode).one();
    }

    @Override
    public boolean updatePayoutStatus(IModifyStatusBaseBO modifyStatusBaseBO) {
        return modifyStatusById(modifyStatusBaseBO, PayoutDO::getPayoutId, PayoutDO::getApplyStatus);
    }

    @Override
    public List<PayoutDO> listAuditPayout(GetAuditApplyBO bo) {
        return lambdaQuery().eq(PayoutDO::getApplyStatus, bo.getApplyStatus())
                .ge(PayoutDO::getApplyTime, bo.getTime()).le(PayoutDO::getApplyTime, LocalDate.now()).list();
    }

    @DataScope
    @Override
    public List<String> searchTableField(PayoutFieldDTO param) {
        return baseMapper.searchTableField(param);
    }

    @DataScope
    @Override
    public List<PayoutDO> payoutPage(PayoutPageDTO payout) {
        LambdaQueryWrapper<PayoutDO> query = new LambdaQueryWrapper<>();
        query.eq(payout.getPayoutCode() != null, PayoutDO::getPayoutCode, payout.getPayoutCode());
        query.eq(payout.getApplyStatus() != null, PayoutDO::getApplyStatus, payout.getApplyStatus());
        query.ge(payout.getMinAmount() != null, PayoutDO::getAmount, payout.getMinAmount());
        query.le(payout.getMaxAmount() != null, PayoutDO::getAmount, payout.getMaxAmount());
        query.like(payout.getPhone() != null, PayoutDO::getPhone, payout.getPhone());
        query.like(payout.getPayoutAccount() != null, PayoutDO::getPayoutAccount, payout.getPayoutAccount());
        query.like(payout.getChannel() != null, PayoutDO::getChannel, payout.getChannel());
        query.like(payout.getCreateName() != null, PayoutDO::getCreateName, payout.getCreateName())
                .orderByDesc(PayoutDO::getApplyTime);
        if (payout.isExport())
            return list(query);
        return pageList(payout, query);
    }
}
